package com.cskaoyan.service;


import com.cskaoyan.bean.baseresultVO.BaseData;
import com.cskaoyan.bean.baseresultVO.BaseParam;

import com.cskaoyan.bean.domain.Brand;
import com.cskaoyan.bean.domain.BrandExample;
import com.cskaoyan.mapper.BrandMapper;
import com.cskaoyan.utils.MyUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


import java.util.Date;
import java.util.List;

/**
 * <p>
 * 品牌商表 服务实现类
 * </p>
 *
 * @author looper
 * @since 2021-11-29
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Resource
    BrandMapper mapper;


    @Override
    public BaseData getBrandsByBaseParam(BaseParam baseParam, Integer id, String name) {
        //分页
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());
        String sort = baseParam.getSort();
        String order = baseParam.getOrder();

        //条件查询
        BrandExample example = new BrandExample();
        BrandExample.Criteria criteria = example.createCriteria();
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(0);
        //排序
        example.setOrderByClause(sort + " " + order);
        List<Brand> brandList = mapper.selectByExample(example);
        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);
        Integer total = MyUtils.getInteger(pageInfo.getTotal());
        return new BaseData(pageInfo.getPageNum(), pageInfo.getSize(), total, pageInfo.getPages(), brandList);

    }

    @Override
    public Integer getDeletById(Integer id, Date time) {

        Integer code = mapper.deleteByUpdate(id, time);

        return code;

    }

    @Override
    public Brand getUpdate(Brand brand, Date date) {
        //根据id更新时间
        mapper.updatetime(date, brand.getId());
        //更新数据
        BrandExample brandExample = new BrandExample();
        BrandExample.Criteria criteria = brandExample.createCriteria();
        criteria.andIdEqualTo(brand.getId());
        brand.setId(null);
        mapper.updateByExampleSelective(brand, brandExample);
        //查询数据.返回的为int的删除,接收之后
        Brand brandById = mapper.selectByPrimaryKey(brand.getId());

        return brandById;

    }

    @Override
    public Brand getInsert(Brand brand) {
        brand.setUpdateTime(new Date());
        //插入
        brand.setDeleted(false);
        mapper.insertSelective(brand);


        //根据返回id查询
        Brand brandPrimaryKey = mapper.selectByPrimaryKey(brand.getId());

        return brandPrimaryKey;
    }

    @Override
    public Brand getBrandDetail(Integer id) {
        Brand brand = mapper.selectByPrimaryKey(id);
        return brand;
    }

    @Override
    public BaseData getList(BaseParam baseParam) {
        PageHelper.startPage(baseParam.getPage(), baseParam.getLimit());

        BrandExample example = new BrandExample();
        BrandExample.Criteria criteria = example.createCriteria();

        if (!MyUtils.isEmpty(baseParam.getOrder()) && !MyUtils.isEmpty(baseParam.getOrder())) {
            //二者都不为空,进行排序
            example.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        }
        criteria.andDeletedEqualTo(0);
        List<Brand> brandList = mapper.selectByExample(example);
        PageInfo<Brand> pageInfo = new PageInfo<>(brandList);
        return BaseData.getBack(pageInfo.getSize(),
                pageInfo.getPageNum(), pageInfo.getPages(),
                MyUtils.getInteger(pageInfo.getTotal()), brandList);


    }
}
